package com.sportizz.sport;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;

import com.sportizz.model.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class Br_custom_tweet_list extends BaseAdapter {

	Context context_;
	List<String> tweet_info;
	HashMap<String, String> tweet_details;
	ImageLoader imloder;
	
	public Br_custom_tweet_list(Context context, List<String> tweet_info,
			HashMap<String, String> tweet_details) {

		this.context_ = context;
		this.tweet_info = tweet_info;
		this.tweet_details = tweet_details;

		imloder=new ImageLoader(context_);
	}

	@Override
	public int getCount() {
		return tweet_info.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = null;

		LayoutInflater inflater = (LayoutInflater) context_
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {
			v = inflater.inflate(R.layout.tweet_list, null);

		} else {
			v = convertView;
		}

		Typeface tweet_font = Typeface.createFromAsset(context_.getAssets(),
				"Hel67medium.ttf");
		Typeface tweet_font1 = Typeface.createFromAsset(context_.getAssets(),
				"hel57.ttf");

		ImageView tweet_image = (ImageView) v.findViewById(R.id.image_tweet);
		TextView txt_tweet = (TextView) v.findViewById(R.id.txt_tweet);
		TextView txt_screenname = (TextView) v
				.findViewById(R.id.txt_screenname);

		String[] tweet_data = tweet_info.get(position).split("''");

		txt_tweet.setText(tweet_data[3]);
		txt_tweet.setTypeface(tweet_font);
		txt_screenname.setText(tweet_data[0]);
		txt_screenname.setTypeface(tweet_font1);

		int loader = R.drawable.tweets;
		
		imloder.DisplayImage(tweet_data[2], loader, tweet_image);
		
//		InputStream is1 = null;
//		try {
//			is1 = context_.getResources().getAssets()
//					.open("main_menu/tweets.png");
//			Bitmap res1 = BitmapFactory.decodeStream(is1);
//
//			Bitmap output1 = Bitmap.createBitmap(res1.getWidth(),
//					res1.getHeight(), Bitmap.Config.ARGB_8888);
//			Canvas canvas1 = new Canvas(output1);
//
//			Paint paint = new Paint();
//			Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
//			RectF rectF = new RectF(rect);
//			float roundPx1 = 4f;
//
//			paint.setAntiAlias(true);
//			canvas1.drawARGB(0, 0, 0, 0);
//			paint.setColor(0xff424242);
//			canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
//			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//			canvas1.drawBitmap(res1, rect, rect, paint);
//
//			tweet_image.setImageBitmap(output1);
//			// tweet_image.setScaleType(ScaleType.FIT_CENTER);
//
//			new imageTask(tweet_image).execute(tweet_data[2]);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}

		return v;
	}

	private class imageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView imageView = null;

		imageTask(ImageView iv) {
			this.imageView = iv;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			try {
				return download_Image(params[0]);

			} catch (OutOfMemoryError e) {
				return null;
			}

		}

		@Override
		protected void onPostExecute(Bitmap result) {
			Bitmap output;
			Canvas canvas;
			float roundPx;

			if (result == null) {

				InputStream is = null;
				try {

					result = BitmapFactory.decodeStream(is);

				} catch (Exception e) {
					result = BitmapFactory.decodeResource(
							context_.getResources(), R.drawable.tweets);
				}

				output = Bitmap.createBitmap(result.getWidth(),
						result.getHeight(), Bitmap.Config.ARGB_8888);
				canvas = new Canvas(output);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, result.getWidth(),
						result.getHeight());
				RectF rectF = new RectF(rect);
				roundPx = 4f;

				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(result, rect, rect, paint);

				imageView.setImageBitmap(output);
				imageView.setScaleType(ScaleType.FIT_CENTER);

			} else {
				output = Bitmap.createBitmap(result.getWidth(),
						result.getHeight(), Bitmap.Config.ARGB_8888);
				canvas = new Canvas(output);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, result.getWidth(),
						result.getHeight());
				RectF rectF = new RectF(rect);
				roundPx = 4f;

				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(result, rect, rect, paint);

				imageView.setImageBitmap(output);

			}
			super.onPostExecute(result);
		}

		private Bitmap download_Image(String url) {
			// ---------------------------------------------------
			Bitmap bm = null;
			try {
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
				conn.connect();
				InputStream is = conn.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				bm = BitmapFactory.decodeStream(bis);
				bis.close();
				is.close();
			} catch (Exception e) {
				Log.e("Hub", "Error getting the image from server : ");
				InputStream iss = null;
				try {
					iss = context_.getResources().getAssets()
							.open("main_menu/tweets.png");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				return BitmapFactory.decodeStream(iss);
			}

			return bm;

		}

	}

}
